package com.atguigu.demojuc.chap01;

//资源类
class Ticket{
    private int ticket = 50;

    public synchronized void sale ()
    {
        if (ticket > 0){
            System.out.println(Thread.currentThread().getName() + "\t" + "售卖了第"+ticket+"张票，剩余票数"+(--ticket));
        }

    }
}

/**
 * 例子：卖票程序
 *
 *      多线程编程模板：
 *          上：
 *              高内聚 低耦合
 *              线程 操作 资源类
 */
public class SaleTicket {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(() -> {
            for (int i = 0; i < 51; i++) {
                ticket.sale();
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 51; i++) {
                ticket.sale();
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 51; i++) {
                ticket.sale();
            }
        }, "C").start();
    }
}
